package com.kk.blog.controller.admin;

import com.kk.blog.pojo.User;
import com.kk.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService userService;

    //用户登录判断
    @GetMapping
    public String toLogin(){
        return "admin/login";
    }
    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam  String password,
                        HttpSession session,
                        Model model){
        //去数据库查询
        User user = userService.checkUser(username, password);
        if (user != null){
            user.setPassword(null);
            session.setAttribute("user",user);

            return "admin/index";
        }

        model.addAttribute("msg","用户名或密码错误");
        return "admin/login";

    }

    //用户退出
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";

    }




}
